select * from AccountInfo
select * from BankCard
select * from CardExchange
select * from CardStateChange
select * from CardTransfer


--1.假设刘备取款6000，(添加check约束，设置账户余额必须>=0)，要求：使用事务实现，修改余额和添加取款记录两步操作使用事务
alter table BankCard add constraint CK_Money check(CardMoney>=0)
begin transaction A
declare @Myerror int =0
declare @myerr int = 0
update BankCard set CardMoney = CardMoney-6000 where CardNo ='6225125478544587'
set @myerr += @@error
insert into CardExchange (CardNo,MoneyInBank,MoneyOutBank,ExchangeTime)
values('6225125478544587',0,6000,GETDATE())
set @myerr += @@error
if @Myerror = 0
begin 
	commit transaction
	print'取款成功'
end
else
begin 
rollback transaction
	print'取款失败'
end
--2.刘备向张飞转账1000元，(添加check约束，设置账户余额必须>=0)
alter table BankCard add constraint CK_Money check(CardMoney>=0)
begin transaction
declare @error int = 0
declare @myerry int = 0
update BankCard set CardMoney = CardMoney -1000 where CardNo = '6225125478544587'
set @myerry+=@@ERROR
update BankCard set CardMoney = CardMoney + 1000 where CardNo = '6225547854125656'
set @myerry+=@@ERROR
insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime)
values('6225125478544587','6225547854125656',1000,GETDATE())
set @myerry+=@@ERROR
if @error = 0
	begin
		commit
		print '转账成功'
	end
else
	begin
		rollback
		print '转账失败'		
	end